//
//  ReadWriteDataClass.m
//  MartketNote
//
//  Created by Nguyen Minh Giang on 12/14/13.
//  Copyright (c) 2013 Nguyen Minh Giang. All rights reserved.
//

#import "ReadWriteDataClass.h"

@implementation ReadWriteDataClass

+ (NSManagedObjectContext *)managedObjectContext {
    NSManagedObjectContext *context = nil;
    id delegate = [[UIApplication sharedApplication] delegate];
    if ([delegate performSelector:@selector(managedObjectContext)]) {
        context = [delegate managedObjectContext];
    }
    return context;
}

#pragma mark - table Products
+ (NSMutableArray *)getDataProducts
{
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:ENT_PRODUCTS];
    return [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
}

+ (void)insertNewProduct:(ProductInfo *)info
{
    NSManagedObjectContext *context = [self managedObjectContext];
    NSManagedObject *new = [NSEntityDescription insertNewObjectForEntityForName:ENT_PRODUCTS inManagedObjectContext:context];
    [new setValue:[NSNumber numberWithInt:info.idProduct] forKey:ATT_ID];
    [new setValue:info.product forKey:ATT_PRODUCT];
    [new setValue:info.detail forKey:ATT_DETAIL];
    [new setValue:[NSNumber numberWithBool:info.isDone] forKey:ATT_STATUS];
    
    NSError *error = nil;
    // Save the object to persistent store
    if (![context save:&error]) {
        NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
    }
}

+ (void)updateProduct:(ProductInfo *)info
{
    NSManagedObjectContext *context = [self managedObjectContext];
    
    NSMutableArray *productList = [self getDataProducts];
    for(NSManagedObject *obj in productList)
    {
        int idProduct = [[obj valueForKey:ATT_ID] intValue];
        if(idProduct == info.idProduct)
        {
            [obj setValue:info.product forKey:ATT_PRODUCT];
            [obj setValue:info.detail forKey:ATT_DETAIL];
            [obj setValue:[NSNumber numberWithBool:info.isDone] forKey:ATT_STATUS];
            break;
        }
    }
    
    NSError *error = nil;
    // Save the object to persistent store
    if (![context save:&error]) {
        NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
    }
}

+ (void)deleteProduct:(ProductInfo *)info
{
    NSManagedObjectContext *context = [self managedObjectContext];
    NSMutableArray *productList = [self getDataProducts];
    for(NSManagedObject *obj in productList)
    {
        int idProduct = [[obj valueForKey:ATT_ID] intValue];
        if(idProduct == info.idProduct)
        {
            [context deleteObject:obj];
            break;
        }
    }
    NSError *error = nil;
    // Save the object to persistent store
    if (![context save:&error]) {
        NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
    }
}

+ (void)insertNewProductWithID:(int)idPro name:(NSString *)name quantity:(float)quantity unit:(NSString *)unit detail:(NSString *)detail status:(BOOL)status
{
    NSManagedObjectContext *context = [self managedObjectContext];
    NSManagedObject *new = [NSEntityDescription insertNewObjectForEntityForName:ENT_PRODUCTS inManagedObjectContext:context];
    [new setValue:[NSNumber numberWithInt:idPro] forKey:ATT_ID];
    [new setValue:name forKey:ATT_PRODUCT];
    [new setValue:[NSNumber numberWithFloat:quantity] forKey:ATT_QUANTITY];
    [new setValue:unit forKey:ATT_UNIT];
    [new setValue:detail forKey:ATT_DETAIL];
    [new setValue:[NSNumber numberWithBool:status] forKey:ATT_STATUS];
    
    NSError *error = nil;
    // Save the object to persistent store
    if (![context save:&error]) {
        NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
    }
}

+ (void)updateProductWithID:(NSManagedObject *)product newName:(NSString *)newName newQuantity:(float)newQuantity newUnit:(NSString *)newUnit newDetail:(NSString *)newDetail newStatus:(BOOL)newStatus
{
    NSManagedObjectContext *context = [self managedObjectContext];
    
    NSMutableArray *productList = [self getDataProducts];
    int productID = [[product valueForKey:ATT_ID] intValue];
    for(NSManagedObject *obj in productList)
    {
        int idPro = [[obj valueForKey:ATT_ID] intValue];
        if(idPro == productID)
        {
            [obj setValue:newName forKey:ATT_PRODUCT];
            [obj setValue:[NSNumber numberWithFloat:newQuantity] forKey:ATT_QUANTITY];
            [obj setValue:newUnit forKey:ATT_UNIT];
            [obj setValue:newDetail forKey:ATT_DETAIL];
            [obj setValue:[NSNumber numberWithBool:newStatus] forKey:ATT_STATUS];
            break;
        }
    }
    
    NSError *error = nil;
    // Save the object to persistent store
    if (![context save:&error]) {
        NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
    }
}

+ (void)deleteManagedObject:(NSManagedObject *)managedObject
{
    NSManagedObjectContext *context = [self managedObjectContext];
    [context deleteObject:managedObject];
    
    NSError *error = nil;
    // Save the object to persistent store
    if (![context save:&error]) {
        NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
    }
}

+ (void)deleteAllData
{
    NSManagedObjectContext *context = [self managedObjectContext];
    NSMutableArray *productList = [self getDataProducts];
    for(NSManagedObject *obj in productList)
    {
        [context deleteObject:obj];
    }
    NSError *error = nil;
    // Save the object to persistent store
    if (![context save:&error]) {
        NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
    }
    
//    NSLog(@"Delated");
}

+ (void)updateStatusProduct:(NSManagedObject *)product
{
    NSManagedObjectContext *context = [self managedObjectContext];
    
    NSMutableArray *productList = [self getDataProducts];
    int productID = [[product valueForKey:ATT_ID] intValue];
    for(NSManagedObject *obj in productList)
    {
        int idPro = [[obj valueForKey:ATT_ID] intValue];
        if(idPro == productID)
        {
//            [obj setValue:newName forKey:ATT_PRODUCT];
//            [obj setValue:newDetail forKey:ATT_DETAIL];
            
            [obj setValue:[NSNumber numberWithBool:![[obj valueForKey:ATT_STATUS] boolValue]] forKey:ATT_STATUS];
            break;
        }
    }
    
    NSError *error = nil;
    // Save the object to persistent store
    if (![context save:&error]) {
        NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
    }
}

+ (int)getProductID
{
    NSMutableArray *productList = [self getDataProducts];
    if([productList count] <= 0)
        return 0;
    int idPro = 0;
    BOOL isFinish = FALSE;
    while(!isFinish)
    {
        BOOL isHave = FALSE;
        for(NSManagedObject *obj in productList)
        {
            int idProduct = [[obj valueForKey:ATT_ID] intValue];
            if(idProduct == idPro)
            {
                isHave = TRUE;
                break;
            }
        }
        if(isHave)
        {
            idPro ++;
            isHave = FALSE;
        }
        else
        {
            isFinish = TRUE;
        }
    }
    
    return idPro;
}


#pragma mark - table Settings
+ (NSMutableArray *)getDataSettings
{
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:ENT_SETTINGS];
    return [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
}

+ (int)getLanguage
{
//    NSLog(@"get language");
    NSMutableArray *settings = [self getDataSettings];
    if([settings count] <= 0)
        [self insertNewSettingsWithLanguage:LANGUAGE_EN];
    else
    {
        NSManagedObject *obj = [settings objectAtIndex:0];
        return [[obj valueForKey:ATT_LANGUAGE] intValue];
    }
    return LANGUAGE_EN;
}

+ (void)insertNewSettingsWithLanguage:(int)language
{
//    NSLog(@"insert setting");
    NSManagedObjectContext *context = [self managedObjectContext];
    NSManagedObject *new = [NSEntityDescription insertNewObjectForEntityForName:ENT_SETTINGS inManagedObjectContext:context];
    [new setValue:[NSNumber numberWithInt:language] forKey:ATT_LANGUAGE];
    
    NSError *error = nil;
    // Save the object to persistent store
    if (![context save:&error]) {
        NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
    }
}

+ (void)updateLanguageWithNewLanguage:(int)newLanguage
{
//    NSLog(@"Update language");
    NSManagedObjectContext *context = [self managedObjectContext];
    
    NSMutableArray *settingList = [self getDataSettings];
    NSManagedObject *obj = [settingList objectAtIndex:0];
    [obj setValue:[NSNumber numberWithInt:newLanguage] forKey:ATT_LANGUAGE];
    
    NSError *error = nil;
    // Save the object to persistent store
    if (![context save:&error]) {
        NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
    }
}


@end
